<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>文章列表--layui后台管理模板 2.0</title>
	<meta name="renderer" content="webkit">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="apple-mobile-web-app-status-bar-style" content="black">
	<meta name="apple-mobile-web-app-capable" content="yes">
	<meta name="format-detection" content="telephone=no">
	<link rel="stylesheet" href="resources/layui/css/layui.css" media="all" />
	<link rel="stylesheet" href="resources/css/public.css" media="all" />
</head>
<body class="childrenBody">
<!--查询条件-->
<div class="layui-collapse" id="search">
	<blockquote class="layui-elem-quote quoteBox">
		<form class="layui-form" >
		    <div class="layui-form-item layui-row">
		        <div class="layui-inline layui-col-md5">
                    <label class="layui-form-label">结算单编号</label>
                	<div class="layui-input-block">
                	    <input type="hidden" name="providerId" id="providerId">
                		<input type="text" name="closeNum" class="layui-input searchVal"/>
                	</div>
                </div>
                <div class="layui-inline layui-col-md5">
                    <label class="layui-form-label">结算单名称</label>
                	<div class="layui-input-block">
                		<input type="text" name="closeName" class="layui-input searchVal"/>
                	</div>
                </div>
            </div>
            <div class="layui-form-item layui-row">
                <div class="layui-inline layui-col-md5">
                    <label class="layui-form-label">结算状态</label>
                	<div class="layui-input-block">
                	    <select name="closeState" id="closeState" lay-filter="closeState" >
                            <option>请选择结算状态：</option>
                		</select>
                	</div>
                </div>
                <div class="layui-inline layui-col-md6">
                    <label class="layui-form-label">结算时间</label>
                	<div class="layui-input-inline">
                		<input type="text" class="layui-input" name="startTime" id="startCreateTime" autocomplete="off" placeholder="yyyy-MM-dd">
                	</div>
                	<label class="layui-form-label" style="width: 5px">-</label>
                	<div class="layui-input-inline">
                    	<input type="text" class="layui-input" name="endTime" id="endCreateTime" autocomplete="off" placeholder="yyyy-MM-dd">
                    </div>
                </div>
            </div>
            <div class="layui-form-item layui-row">
                <div class="layui-inline layui-col-md5">
                    <label class="layui-form-label">下单医院</label>
                	<div class="layui-input-block">
                	    <input type="hidden" id="hospitalId">
                		<input type="text" name="hospitalName" class="layui-input searchVal"/>
                	</div>
                </div>
                <div class="layui-inline layui-col-md6">
                    <label class="layui-form-label">采购时间</label>
                	<div class="layui-input-inline">
                		<input type="text" class="layui-input" name="createTime" id="createTime" autocomplete="off" placeholder="yyyy-MM-dd">
                	</div>
                	<label class="layui-form-label" style="width: 5px">-</label>
                	<div class="layui-input-inline">
                    	<input type="text" class="layui-input" name="overTime" id="overTime" placeholder="yyyy-MM-dd" autocomplete="off" >
                    </div>
                </div>
            </div>
            <div class="layui-form-item layui-row">
                <div class="layui-inline layui-col-md5">
                    <label class="layui-form-label">对应采购单编号</label>
                	<div class="layui-input-block">
                	    <input type="hidden" id="purchaseId">
                		<input type="text" name="purchaseNum" class="layui-input searchVal" placeholder="请输入采购单编号" />
                	</div>
                </div>
                <div class="layui-inline layui-col-md5">
                    <label class="layui-form-label">对应采购单名称</label>
                	<div class="layui-input-block">
                		<input type="text" name="purchaseName" class="layui-input searchVal" placeholder="请输入采购单名称" />
                	</div>
                </div>
            </div>
            <div class="layui-form-item layui-row">
                <div class="layui-inline layui-col-md5">
                    <label class="layui-form-label">药品流水号</label>
                	<div class="layui-input-block">
                		<input type="text" name="serialNumber" class="layui-input searchVal" />
                	</div>
                </div>
                <div class="layui-inline layui-col-md5">
                    <label class="layui-form-label">通用名</label>
                	<div class="layui-input-block">
                		<input type="text" name="commonName" class="layui-input searchVal" />
                	</div>
                </div>
            </div>
            <div class="layui-form-item layui-row">
                <div class="layui-inline layui-col-md5">
                    <label class="layui-form-label">剂型</label>
                	<div class="layui-input-block">
                		<input type="text" name="dosageForm" class="layui-input searchVal"/>
                	</div>
                </div>
                <div class="layui-inline layui-col-md5">
                    <label class="layui-form-label">规格</label>
                	<div class="layui-input-block">
                		<input type="text" name="size" class="layui-input searchVal"/>
                	</div>
                </div>
            </div>
            <div class="layui-form-item layui-row">
                <div class="layui-inline layui-col-md5">
                    <label class="layui-form-label">单位</label>
                	<div class="layui-input-block">
                		<input type="text" name="unit" class="layui-input searchVal"/>
                	</div>
                </div>
                <div class="layui-inline layui-col-md5">
                    <label class="layui-form-label">转换系数</label>
                	<div class="layui-input-block">
                		<input type="text" name="factor" class="layui-input searchVal"/>
                	</div>
                </div>
            </div>
            <div class="layui-form-item layui-row">
                <div class="layui-inline layui-col-md5">
                    <label class="layui-form-label">生产企业</label>
                	<div class="layui-input-block">
                		<input type="text" name="productionName" class="layui-input searchVal"/>
                	</div>
                </div>
                <div class="layui-inline layui-col-md5">
                    <label class="layui-form-label">商品名</label>
                	<div class="layui-input-block">
                		<input type="text" name="commodityName" class="layui-input searchVal" />
                	</div>
                </div>
            </div>
            <div class="layui-form-item layui-row">
                <div class="layui-inline layui-col-md5">
                    <label class="layui-form-label">质量层次</label>
                	<div class="layui-input-block">
                		<select name="quality" id="quality" lay-filter="quality" >
                            <option>请选择质量层次：</option>
                        </select>
                	</div>
                </div>
                <div class="layui-inline layui-col-md5">
                    <label class="layui-form-label">药品类别</label>
                	<div class="layui-input-block">
                		<select name="drugCategory" id="drugCategory" lay-filter="drugCategory" >
                            <option>请选择药品类别：</option>
                        </select>
                	</div>
                </div>
            </div>
            <div class="layui-form-item layui-center">
                <div class="layui-input-block">
                  <button type="submit" class="layui-btn" lay-submit="" lay-filter="search">查询</button>
                  <button type="submit" class="layui-btn layui-btn-primary" lay-submit="" lay-filter="close">导出</button>
                </div>
              </div>
		</form>
	</blockquote>
</div>
<!--数据表格-->
<div>
    <table class="layui-hide" id="closeTable" lay-filter="closeTable"></table>
    <div id="toolBars" style="display: none">
        <button type="button" lay-event="add" class="layui-btn layui-btn-sm"><span class="layui-icon layui-icon-add-1"></span>确认结算</button>
    </div>
    <div id="rowBars" style="display: none">
        <button type="button" lay-event="show" class="layui-btn layui-btn-sm layui-btn-warm"><span class="layui-icon layui-icon-search"></span>查看</button>
        <button type="button" lay-event="update" class="layui-btn layui-btn-sm layui-bg-green"><span class="layui-icon layui-icon-edit"></span>修改</button>
        <button type="button" lay-event="delete" class="layui-btn layui-btn-sm layui-btn-danger"><span class="layui-icon layui-icon-delete"></span>删除</button>
    </div>
</div>
<script type="text/javascript" src="resources/layui/layui.js"></script>
<script>
    layui.use(['form','jquery','layer','laydate','element','table'],function() {
        var form = layui.form;
        var laydate = layui.laydate;
        var layer = layui.layer;
        var $ = layui.jquery;
        var table = layui.table;
        //渲染下拉框
        //结算状态
        select($("#closeState"),5,1);
        //质量层次
        select($("#quality"),7,0);
        //药品类别
        selectDrugCatalog($("#drugCategory"));
        //渲染时间
        laydate.render({
            elem: '#startCreateTime'
            ,type: 'datetime'
        });
        laydate.render({
            elem: '#endCreateTime'
            ,type: 'datetime'
        });
        laydate.render({
            elem: '#createTime'
            ,type: 'datetime'
        });
        laydate.render({
            elem: '#overTime'
            ,type: 'datetime'
        });
        //采购商id
        var providerId;
        $.ajax({
            url:"/getSessionUnitId",
            async:false,
            type:"post",
            success:function(unitId) {
                providerId = unitId;
                $("#providerId").val(providerId);
            }
        });
        //记录结算状态
        var closeState = 1;
        //监听采购状态的下拉框
        form.on('select(closeState)',function(data) {
            closeState = data.value;
        });
        if(providerId==undefined||providerId==""){
            window.location.href = "/toNoPermission";
            return;
        }
        //渲染数据表格
        var tableIns = table.render({
            elem:"#closeTable",
            url:"/providerCloseDetail/getAllClose",
            page:true,
            id:"closeTableId",
            where:{providerId:providerId,closeState:closeState},
            // totalRow: true,
            toolbar:"#toolBars",
            cols:[
                [
                    {field:'id', title:'ID',  fixed: 'left', unresize: true,width:'80' ,sort: true,align:'center'},
                    {type:'checkbox',fixed:'center'},
                    {field:'close.closeNum', title:'结算单编号',align:'center',width:'150',templet:function(res) {
                       return res.providerClose.closeNum==null?"":res.providerClose.closeNum;
                    }},
                    {field:'close.closeName', title:'结算单名称',align:'center',width:'150',templet:function(res) {
                       return res.providerClose.closeName==null?"":res.providerClose.closeName;
                    }},
                    {field:'purchase.hospitalName', title:'下单医院',align:'center',width:'150',templet:function(res) {
                        return res.purchase.hospitalName==null?"":res.purchase.hospitalName;
                    }},
                    {field:'purchase.createTime', title:'开始采购时间',align:'center',width:'150',templet:function(res) {
                        return res.purchase.createTime==null?"":res.purchase.createTime.substring(0,11);
                    }},
                    {field:'purchase.overTime', title:'结束采购时间',align:'center',width:'150',templet:function(res) {
                        return res.purchase.overTime==null?"":res.purchase.overTime.substring(0,11);
                    }},
                    {field:'purchase.purchaseNum', title:'对应采购单编号',align:'center',width:'150',templet:function(res) {
                        return res.purchase.purchaseNum==null?"":res.purchase.purchaseNum;
                    }},
                    {field:'purchase.purchaseName', title:'对应采购单名称',align:'center',width:'150',templet:function(res) {
                        return res.purchase.purchaseName==null?"":res.purchase.purchaseName;
                    }},
                    {field:'drugMessage.serialNumber', title:'流水号',align:'center',width:'150',templet:function(res) {
                       return res.drugMessage.serialNumber==null?"":res.drugMessage.serialNumber
                    }},
                    {field:'drugMessage.commonName', title:'通用名',align:'center',width:'150',templet:function(res) {
                       return res.drugMessage.commonName==null?"":res.drugMessage.commonName
                    }},
                    {field:'drugMessage.commodityName', title:'商品名',align:'center',width:'150',templet:function(res) {
                        console.log(res);
                       return res.drugMessage.commodityName==null?"":res.drugMessage.commodityName;
                    }},
                    {field:'drugMessage.dosageForm', title:'剂型',align:'center',width:'150',templet:function(res) {
                       return res.drugMessage.dosageForm==null?"":res.drugMessage.dosageForm;
                    }},
                    {field:'drugMessage.size', title:'规格',align:'center',width:'150',templet:function(res) {
                       return res.drugMessage.size==null?"":res.drugMessage.size;
                    }},
                    {field:'drugMessage.unit', title:'单位',align:'center',width:'150',templet:function(res) {
                       return res.drugMessage.unit==null?"":res.drugMessage.unit;
                    }},
                    {field:'drugMessage.factor', title:'转换系数',align:'center',width:'150',templet:function(res) {
                       return res.drugMessage.factor==null?"":res.drugMessage.factor;
                    }},
                    {field:'drugMessage.quality', title:'质量层次',align:'center',width:'150',templet:function(res) {
                       return res.drugMessage==undefined?"":
                       res.drugMessage.quality==1?"国家一类新药":
                       res.drugMessage.quality==2?"国家科学进步奖":
                       res.drugMessage.quality==3?"专利":
                       res.drugMessage.quality==4?"原研":
                       res.drugMessage.quality==5?"单独定价":"";
                    }},
                    {field:'drugMessage.factor', title:'生产企业',align:'center',width:'150',templet:function(res) {
                       return res.drugMessage.factor==null?"":res.drugMessage.factor;
                    }},
                    {field:'drugMessage.factor', title:'中标价',align:'center',width:'150',templet:function(res) {
                       return res.drugMessage.factor==null?"":res.drugMessage.factor;
                    }},
                    {field:'drugMessage.factor', title:'交易价',align:'center',width:'150',templet:function(res) {
                       return res.drugMessage.factor==null?"":res.drugMessage.factor;
                    }},
                    {field:'drugMessage.factor', title:'采购量',align:'center',width:'150',templet:function(res) {
                       return res.drugMessage.factor==null?"":res.drugMessage.factor;
                    }},
                    {field:'drugMessage.factor', title:'采购金额',align:'center',width:'150',templet:function(res) {
                       return res.drugMessage.factor==null?"":res.drugMessage.factor;
                    }},
                    {field:'drugMessage.factor', title:'入库量',align:'center',width:'150',templet:function(res) {
                       return res.drugMessage.factor==null?"":res.drugMessage.factor;
                    }},
                    {field:'drugMessage.factor', title:'入库金额',align:'center',width:'150',templet:function(res) {
                       return res.drugMessage.factor==null?"":res.drugMessage.factor;
                    }},
                    {field:'drugMessage.factor', title:'药品批号',align:'center',width:'150',templet:function(res) {
                       return res.drugMessage.factor==null?"":res.drugMessage.factor;
                    }},
                    {field:'drugMessage.factor', title:'药品有效期',align:'center',width:'150',templet:function(res) {
                       return res.drugMessage.factor==null?"":res.drugMessage.factor;
                    }},
                    {field:'drugMessage.factor', title:'退货量',align:'center',width:'150',templet:function(res) {
                       return res.drugMessage.factor==null?"":res.drugMessage.factor;
                    }},
                    {field:'drugMessage.factor', title:'退货金额',align:'center',width:'150',templet:function(res) {
                       return res.drugMessage.factor==null?"":res.drugMessage.factor;
                    }},
                    {field:'drugMessage.factor', title:'结算量',align:'center',width:'150',templet:function(res) {
                       return res.drugMessage.factor==null?"":res.drugMessage.factor;
                    }},
                    {field:'drugMessage.factor', title:'结算金额',align:'center',width:'150',templet:function(res) {
                       return res.drugMessage.factor==null?"":res.drugMessage.factor;
                    }},
                    {field:'drugMessage.factor', title:'结算状态',align:'center',width:'150',templet:function(res) {
                       return res.drugMessage.factor==null?"":res.drugMessage.factor;
                    }}
                    //TODO 不完整，需要医院交易明细配合
                ]
            ]
        });
        //监听表单事件
        form.on("submit(search)",function(data) {
            tableIns.reload({
                where:data.field
            })
            return false;
        });
        //监听表单导出
        form.on('submit(close)',function() {
             var checkStatus = table.checkStatus('closeTableId'); //idTest 即为基础参数 id 对应的值
             if(checkStatus.data.length == 0){
                 layer.msg("请先勾选");
                 return false;
             }
             table.exportFile(tableIns.config.id,checkStatus.data,'xls');
             return false;
        });
        //监听头部工具栏事件
        table.on('toolbar(closeTable)',function(obj) {
            switch (obj.event) {
                case 'add':
                    openAdd();
                    break;
            }
        });
        //确认结算，更改结算状态
        function openAdd() {
            var checkStatus = table.checkStatus('closeTableId'); //idTest 即为基础参数 id 对应的值
            if(checkStatus.data.length == 0){
                layer.msg("请先勾选");
                return;
            }
            var close = new Array();
            $.each(checkStatus.data,function(index,item) {
                var app = {
                    id:item.id,
                    closeState:2
                };
                console.log(item);
                close.push(app);
            })
            $.post("/providerCloseDetail/update",{close:JSON.stringify(close)},function(data) {
                layer.msg(data.msg);
                tableIns.reload();
            })
        }
        function select(item,dateType,num) {
            $.get("/getDataDictionary",{dateType:dateType},function(data) {
                var html = "<option value='0'>请选择</option>";
                $.each(data.data,function(index,item) {
                    if(item.dataNum==num){
                        html += "<option selected value='"+item.dataNum+"'>"+item.dataName+"</option>";
                    }else {
                        html += "<option value='"+item.dataNum+"'>"+item.dataName+"</option>";
                    }
                })
                item.empty();
                item.append(html);
                // $("#purchaseState").html(html);
                form.render('select');
            })
        }
        function selectDrugCatalog(item) {
            $.get("/getDrugCategory",function(data) {
                var html = "<option value='0'>请选择药品类别：</option>";
                $.each(data,function(index,item) {
                    html += "<option value='"+item.id+"'>"+item.category+"</option>";
                })
                item.html(html);
                form.render('select');
            })
        }
    });
</script>
</body>